package com.cskaoyan.controller.mallController;


import com.cskaoyan.model.bean.Category;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.mall.CategoryHasChildrenVo;
import com.cskaoyan.model.vo.mall.CategoryL1Vo;
import com.cskaoyan.service.mall.MallService;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@RequiresPermissions("admin:category:*")
public class MallcategoryController {


    private ObjectMapper objectMapper = new ObjectMapper();

    {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }
    

    @Autowired
    MallService mallService;

    @RequestMapping("/list")
    public BaseRespVo getCategoryList() {
        List<CategoryHasChildrenVo> categoryList = mallService.getCategoryList();

        return BaseRespVo.ok(categoryList);
    }

    @RequestMapping("/l1")
    public BaseRespVo getL1List() {
        List<CategoryL1Vo> categoryList = mallService.getL1List();
        return BaseRespVo.ok(categoryList);
    }

    @RequestMapping("/create")
    public BaseRespVo categoryCreat(@RequestBody Category category) {
        int i = mallService.categoryCreat(category);

        if (i == 1) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(404,"创建失败");
        }
    }

    @RequestMapping("/update")
    public BaseRespVo categoryUpdate(@RequestBody Category category) {
        int i = mallService.categoryUpdate(category);

        if (i == 1) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(404,"更新种类失败");
        }
    }
    @RequestMapping("/delete")
    public BaseRespVo categoryDelete(@RequestBody Category category) {
        int i = mallService.categoryDelete(category.getId());
        if (i == 1) {
            return BaseRespVo.ok();
        } else {
            return BaseRespVo.error(404,"删除种类失败");
        }
    }

}
